package com.zhengshan.demo.unit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * todo
 *
 * @author zhengshan
 * @Date 2023/10/19 12:53
 */
public class RedisUnit {

    @Autowired
    private RedisTemplate redisTemplate;


    public void setStringRed(String key, String value) {
        ValueOperations ops = redisTemplate.opsForValue();
        ops.set(key, value);
    }

    public String getStringRed(String key) {
        ValueOperations ops = redisTemplate.opsForValue();
        return String.valueOf(ops.get(key));
    }


    public void isConnected() {
        //1.构造一个jedis连接池
        JedisPool pool = new JedisPool("101.35.209.155");
        //2.从Jedis连接池来获取一个连接
        Jedis jedis = pool.getResource();
        jedis.auth("redis123456");
        //3.Jedis操作
        try {
            String ping = jedis.ping();
            System.out.println("ping = " + ping);
            jedis.set("k1", "v1");
            jedis.flushAll();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //4.归还连接
            jedis.close();
        }

    }
}
